package com.xiaofeng.pochi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaofeng.pochi.common.Page;
import com.xiaofeng.pochi.mapper.ShopBrandMapper;
import com.xiaofeng.pochi.mapper.ShopPackMapper;
import com.xiaofeng.pochi.mapper.ShopProductMapper;
import com.xiaofeng.pochi.mapper.ShopProductPackMapper;
import com.xiaofeng.pochi.pojo.*;
import com.xiaofeng.pochi.pojo.dto.ShopPackDto;
import com.xiaofeng.pochi.pojo.vo.SysUserVo;
import com.xiaofeng.pochi.service.ShopBrandService;
import com.xiaofeng.pochi.service.ShopPackService;
import com.xiaofeng.pochi.utils.IdWorker;
import com.xiaofeng.pochi.utils.ShiroUtils;
import com.xiaofeng.pochi.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/08/13/22:31
 * @Description:
 */
@Service
public class ShopPackServiceImpl extends ServiceImpl<ShopPackMapper, ShopPack> implements ShopPackService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private ShopPackMapper shopPackMapper;

    @Autowired
    private ShopProductMapper shopProductMapper;

    @Autowired
    private ShopProductPackMapper shopProductPackMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ShopPackDto shopPackDto) {
        // 拷贝属性
        ShopPack shopPack = new ShopPack();
        BeanUtils.copyProperties(shopPackDto, shopPack);
        LoginUser loginUser = ShiroUtils.getLoginUser();
        shopPack.setCreateBy(loginUser.getUsername());
        long packCode = idWorker.nextId();
        shopPack.setId(packCode);
        int productCount = 0;
        List<ShopProduct> productList = shopPackDto.getProductList();
        if (!CollectionUtils.isEmpty(productList)) {
            productCount = shopPackDto.getProductList().size();
        }
        shopPack.setProductCount(productCount);
        // 保存
        shopPackMapper.save(shopPack);
        if (!CollectionUtils.isEmpty(productList)) {
            // 保存商品套装关联表
            List<Long> productIds = productList.stream().map(ShopProduct::getId).collect(Collectors.toList());
            List<ShopProduct> products = shopProductMapper.getByIds(productIds);
            // 构造ShopProductPack
            List<ShopProductPack> packList = products.stream().map(e -> {
                ShopProductPack shopProductPack = new ShopProductPack();
                shopProductPack.setProductId(e.getId());
                shopProductPack.setPackCode(packCode);
                shopProductPack.setPrice(e.getPrice());
                shopProductPack.setStock(e.getStock());
                shopProductPack.setLowStock(e.getLowStock());
                shopProductPack.setSpecName(e.getSpecs());
                shopProductPack.setProductName(e.getName());
                return shopProductPack;
            }).collect(Collectors.toList());
            shopProductPackMapper.saveBatch(packList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ShopPackDto shopPackDto) {
        long packCode = shopPackDto.getId();
        // 拷贝属性
        ShopPack shopPack = new ShopPack();
        BeanUtils.copyProperties(shopPackDto, shopPack);
        int productCount = 0;
        List<ShopProduct> productList = shopPackDto.getProductList();
        if (!CollectionUtils.isEmpty(productList)) {
            productCount = shopPackDto.getProductList().size();
        }
        shopPack.setProductCount(productCount);
        // 修改
        shopPackMapper.update(shopPack);
        // 删除关联表数据
        shopProductPackMapper.deleteByPackCode(packCode);
        if (!CollectionUtils.isEmpty(productList)) {
            // 保存商品套装关联表
            List<Long> productIds = productList.stream().map(ShopProduct::getId).collect(Collectors.toList());
            List<ShopProduct> products = shopProductMapper.getByIds(productIds);
            // 构造ShopProductPack
            List<ShopProductPack> packList = products.stream().map(e -> {
                ShopProductPack shopProductPack = new ShopProductPack();
                shopProductPack.setProductId(e.getId());
                shopProductPack.setPackCode(packCode);
                shopProductPack.setPrice(e.getPrice());
                shopProductPack.setStock(e.getStock());
                shopProductPack.setLowStock(e.getLowStock());
                shopProductPack.setSpecName(e.getSpecs());
                if(StringUtils.isBlank(shopProductPack.getSpecName())){
                    shopProductPack.setSpecName(e.getName());
                }
                shopProductPack.setProductName(e.getName());
                return shopProductPack;
            }).collect(Collectors.toList());
            shopProductPackMapper.saveBatch(packList);
        }
    }

    @Override
    public ShopPackDto getById(Long id) {
        ShopPack pack = shopPackMapper.get(id);
        ShopPackDto shopPackDto = new ShopPackDto();
        BeanUtils.copyProperties(pack,shopPackDto);
        //根据id查询套装
        List<ShopProductPack> packList = shopProductPackMapper.getByPackCode(id);
        if(!CollectionUtils.isEmpty(packList)) {
            List<Long> productIds = packList.stream().map(ShopProductPack::getProductId).collect(Collectors.toList());
            List<ShopProduct> productList = shopProductMapper.getByIds(productIds);
            shopPackDto.setProductList(productList);
        }
        return shopPackDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        shopPackMapper.delete(id);
        shopProductPackMapper.deleteByPackCode(id);
    }

    @Override
    public Page<ShopPack> getByPage(Page<ShopPack> page) {
        List<ShopPack> list = shopPackMapper.getByPage(page);
        Integer totalCount = shopPackMapper.countByPage(page);
        page.setList(list);
        page.setTotalCount(totalCount);
        return page;
    }

    @Override
    public List<ShopProductPackVo> getByProductId(Long productId) {
        //先获取套装编号
        ShopProductPack productPack = shopProductPackMapper.getByProductId(productId);
        ShopProduct product = shopProductMapper.getInfoById(productId);
        if (productPack == null){
            List<ShopProductPackVo> resultList = new ArrayList<>(1);
            // 说明该商品没有加入任何套装。将该商品自己视为一个套装
            ShopProductPackVo packVo = new ShopProductPackVo();
            packVo.setProductId(productId);
            packVo.setPic(product.getPic());
            packVo.setPrice(product.getPrice());
            packVo.setProductName(product.getName());
            packVo.setSpecName(product.getSpecs());
            packVo.setStock(product.getStock());
            if (StringUtils.isBlank(packVo.getSpecName())) {
                packVo.setSpecName(packVo.getProductName());
            }
            resultList.add(packVo);
            return resultList;
        }
        Long packCode = productPack.getPackCode();
        // 商品套装编号不为空
        // 根据套装编号查询所有商品
        List<ShopProductPack> productPackList = shopProductPackMapper.getByPackCode(packCode);
        //取出所有商品编号
        List<Long> productIds = productPackList.stream().map(ShopProductPack::getProductId).collect(Collectors.toList());
        //根据商品编号查询所有商品
        List<ShopProduct> productList = shopProductMapper.getByIds(productIds);
        //转换成vo
        return productList.stream().map(e -> {
            ShopProductPackVo packVo = new ShopProductPackVo();
            packVo.setProductId(e.getId());
            packVo.setPic(e.getPic());
            packVo.setPackCode(packCode);
            packVo.setPrice(e.getPrice());
            packVo.setProductName(e.getName());
            packVo.setSpecName(e.getSpecs());
            packVo.setStock(e.getStock());
            if (StringUtils.isBlank(packVo.getSpecName())) {
                packVo.setSpecName(packVo.getProductName());
            }
            return packVo;
        }).collect(Collectors.toList());
    }

}
